Skip to content

Preserve the .debug_gdb_scripts section #143679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sebastianpoeplau
Copy link

@sebastianpoeplau sebastianpoeplau commented Jul 9, 2025

Make sure that compiler and linker don't optimize the section's contents
away by adding the global holding the data to llvm.used. This
eliminates the need for a volatile load in the main shim; since the LLVM
codegen backend is the only implementer of the corresponding trait
function, remove it entirely.

Pretty printers in dylib dependencies are now emitted by the main crate
instead of the dylib; apart from matching how rlibs are handled, this
approach has the advantage that omit_gdb_pretty_printer_section keeps
working with dylib dependencies.

r? @bjorn3

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 9, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 9, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@sebastianpoeplau sebastianpoeplau force-pushed the preserve-debug-gdb-scripts-section branch 2 times, most recently from 97ac05e to 07de96b Compare July 9, 2025 12:16
@sebastianpoeplau sebastianpoeplau force-pushed the preserve-debug-gdb-scripts-section branch from 07de96b to d54c3ea Compare July 9, 2025 13:20
@bjorn3
Copy link
Member

bjorn3 commented Jul 9, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 9, 2025

📌 Commit d54c3ea has been approved by bjorn3

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 9, 2025
tgross35 added a commit to tgross35/rust that referenced this pull request Jul 10, 2025
…b-scripts-section, r=bjorn3

Preserve the .debug_gdb_scripts section

Make sure that compiler and linker don't optimize the section's contents away by adding the global holding the data to `llvm.used`. This eliminates the need for a volatile load in the main shim; since the LLVM codegen backend is the only implementer of the corresponding trait function, remove it entirely.

r? `@bjorn3`
tgross35 added a commit to tgross35/rust that referenced this pull request Jul 10, 2025
…b-scripts-section, r=bjorn3

Preserve the .debug_gdb_scripts section

Make sure that compiler and linker don't optimize the section's contents away by adding the global holding the data to `llvm.used`. This eliminates the need for a volatile load in the main shim; since the LLVM codegen backend is the only implementer of the corresponding trait function, remove it entirely.

r? ``@bjorn3``
bors added a commit that referenced this pull request Jul 10, 2025
Rollup of 8 pull requests

Successful merges:

 - #140136 (Add an aarch64-msvc build running on ARM64 Windows)
 - #143642 (stdarch subtree update)
 - #143679 (Preserve the .debug_gdb_scripts section)
 - #143707 (Fix `--skip-std-check-if-no-download-rustc`)
 - #143722 (Make some "safe" llvm ops actually sound)
 - #143728 (Resolve refactor: extraction of `finalize_module_binding` and `single_import_can_define_name`)
 - #143742 (Rework borrowing suggestions to use `Expr` instead of just `Span`)
 - #143744 (Properly track the depth when expanding free alias types)

r? `@ghost`
`@rustbot` modify labels: rollup
@tgross35
Copy link
Contributor

Looks like some GDB tests failed #143755 (comment)
@bors r-
@bors2 try jobs=dist-i586-gnu-i586-i686-musl

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 11, 2025
rust-bors bot added a commit that referenced this pull request Jul 11, 2025
…ection, r=<try>

Preserve the .debug_gdb_scripts section

Make sure that compiler and linker don't optimize the section's contents away by adding the global holding the data to `llvm.used`. This eliminates the need for a volatile load in the main shim; since the LLVM codegen backend is the only implementer of the corresponding trait function, remove it entirely.

r? `@bjorn3`
try-job: dist-i586-gnu-i586-i686-musl
@rust-bors
Copy link

rust-bors bot commented Jul 11, 2025

⌛ Trying commit d54c3ea with merge f7ba1a6

To cancel the try build, run the command @bors2 try cancel.

@rust-bors
Copy link

rust-bors bot commented Jul 11, 2025

💔 Test failed

@sebastianpoeplau
Copy link
Author

Looks like some GDB tests failed #143755 (comment)

Interesting, I'm going to have a look. The test passes on my x86_64-unknown-linux-gnu; I'll try to find what's different in the CI job. The name dist-i586-gnu-i586-i686-musl suggests that it's a 32-bit build - where can I find out more? Is there a way to run the same setup locally?

@tgross35
Copy link
Contributor

You might be able to get away with passing --target ... to ./x since these tests should still be able to run on your host. It looks like it fails testing i686-unknown-linux-musl.

If that doesn't repro it for some reason you can replicate the CI setup pretty much exactly by running the docker tests https://rustc-dev-guide.rust-lang.org/tests/docker.html, with the downside that this takes about as long as CI does.

@sebastianpoeplau
Copy link
Author

Hmm, the full log of the CI job led me to believe that it's i586-unknown-linux-gnu, but locally the test passes for that target (running ./x.py test tests/debuginfo/embedded-visualizer.rs --target i586-unknown-linux-gnu). Will try with the musl target 👍

I noticed though that my GDB is version 15, whereas the test log shows GDB 12; maybe that's the reason for the discrepancy. In general, the .debug_gdb_scripts section looks correct to me...

@tgross35
Copy link
Contributor

You're right, it's i586. I must have been reading the wrong line.

##[group]Testing stage2 compiletest suite=debuginfo mode=debuginfo (x86_64-unknown-linux-gnu -> i586-unknown-linux-gnu)

I assume there probably isn't any reason we need to keep a very old gdb version if that makes a difference

@matthiaskrgr
Copy link
Member

@bors rollup=iffy
this may bounce a couple more times

bors added a commit that referenced this pull request Jul 14, 2025
…ection, r=bjorn3

Preserve the .debug_gdb_scripts section

Make sure that compiler and linker don't optimize the section's contents away by adding the global holding the data to `llvm.used`. This eliminates the need for a volatile load in the main shim; since the LLVM codegen backend is the only implementer of the corresponding trait function, remove it entirely.

r? `@bjorn3`
@bors
Copy link
Collaborator

bors commented Jul 14, 2025

⌛ Testing commit 8be915f with merge f7d013e...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 14, 2025

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 14, 2025
@bjorn3
Copy link
Member

bjorn3 commented Jul 14, 2025

Please add an exception to tests/run-make/symbols-all-mangled/rmake.rs. The symbol is actually mangled. It just doesn't start with _ZN or _R like most mangled symbols.

@bors
Copy link
Collaborator

bors commented Jul 14, 2025

☔ The latest upstream changes (presumably #143919) made this pull request unmergeable. Please resolve the merge conflicts.

@sebastianpoeplau sebastianpoeplau force-pushed the preserve-debug-gdb-scripts-section branch from 8be915f to bc1ff10 Compare July 15, 2025 08:34
@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Jul 15, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 15, 2025

This PR modifies run-make tests.

cc @jieyouxu

@sebastianpoeplau
Copy link
Author

Added the exception and rebased on master.

@bjorn3
Copy link
Member

bjorn3 commented Jul 15, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 15, 2025

📌 Commit bc1ff10 has been approved by bjorn3

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 15, 2025
@sebastianpoeplau
Copy link
Author

Hmm, there's some problem with the omit_gdb_pretty_printer_section attribute now, at least in my local tests 😳 Do we get the section from somewhere even if it's omitted from the main crate? I suppose this could happen if the test linked against one of the crate types that are considered leaves in needs_gdb_debug_scripts_section, like dylib. But shouldn't those have had pretty printers before as well? Maybe the linker removed them because there's no main shim in those crate types 🤔

@bjorn3
Copy link
Member

bjorn3 commented Jul 15, 2025

Tests get compiled with -Cprefer-dynamic by default to dynamically link against libstd. Try adding //@ no-prefer-dynamic to the relevant tests.

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 15, 2025
@sebastianpoeplau
Copy link
Author

Try adding //@ no-prefer-dynamic to the relevant tests.

That doesn't seem to help - we get the section from the libstd rlib as well. It seems to be part of all object files inside the archive. I wonder why...

Oh, I think it's because of this bit in library/std/Cargo.toml:

[lib]
crate-type = ["dylib", "rlib"]

The code in needs_gdb_debug_scripts_section checks whether any of the crate types needs the section! That doesn't seem right, but reading the comment in that function made me wonder about the approach more generally:

// To ensure the section __rustc_debug_gdb_scripts_section__ will not create
// ODR violations at link time, this section will not be emitted for rlibs since
// each rlib could produce a different set of visualizers that would be embedded
// in the .debug_gdb_scripts section. For that reason, we make sure that the
// section is only emitted for leaf crates.

This doesn't apply anymore, does it? There's no risk of ODR violations because the symbols get a unique name. The real reason now is that get_or_insert_gdb_debug_scripts_section_global still collects visualizers transitively from all crates. If we keep the transitive collection, shouldn't we drop the emission of the debug script section for dylibs? I'm thinking that they shouldn't have the section because the compiler can see their visualizers when building executables that use them, can't it? I've tested locally that returning false for CrateType::Dylib fixes the debuginfo tests, but I don't know whether it has unwanted side effects.

Alternatively, we could collect visualizers for the current crate only, emit the per-crate list for all crate types, and rely on the linker to merge the .debug_gdb_scripts sections. This would require special handling of gdb_load_rust_pretty_printers.py though - maybe it could be emitted only by libstd?

@sebastianpoeplau
Copy link
Author

the compiler can see their visualizers when building executables that use them

This seems to be incorrect when the dependency is in a dylib crate. I tried a variant of tests/debuginfo/embedded-visualizer.rs where the dependency is built as a dylib instead of an rlib. When I don't emit the .debug_gdb_scripts section for dylibs, the test fails because the dependency's visualizer is missing. It works fine, however, with an rlib, even if there's no .debug_gdb_scripts section in it.

Does this mean that the compiler has more visibility into rlib dependencies than into dylibs?

Make sure that compiler and linker don't optimize the section's contents
away by adding the global holding the data to "llvm.used". This
eliminates the need for a volatile load in the main shim; since the LLVM
codegen backend is the only implementer of the corresponding trait
function, remove it entirely.

Pretty printers in dylib dependencies are now emitted by the main crate
instead of the dylib; apart from matching how rlibs are handled, this
approach has the advantage that `omit_gdb_pretty_printer_section` keeps
working with dylib dependencies.
@sebastianpoeplau sebastianpoeplau force-pushed the preserve-debug-gdb-scripts-section branch from bc1ff10 to 0fb1e12 Compare July 15, 2025 11:36
@sebastianpoeplau
Copy link
Author

sebastianpoeplau commented Jul 15, 2025

Ah, here we go: collect_debugger_visualizers_transitive filters by metadata source, apparently excluding dylibs. If I remove the filter and stop the compiler from emitting .debug_gdb_scripts for dylibs, all debuginfo tests pass 🥳

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-compiletest Area: The compiletest test runner A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-run-make Area: port run-make Makefiles to rmake.rs A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants